package com.raja.servingStation.controller.historyOrder;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.common.constant.MysqlPageVo;
import com.raja.servingStation.common.util.RequestUtil;
import com.raja.servingStation.common.util.ResponseUtil;
import com.raja.servingStation.controller.common.BaseController;
import com.raja.servingStation.entity.FirstInsuranceOrder;
import com.raja.servingStation.entity.RepairOrderDetail;
import com.raja.servingStation.service.ClaimService;
import com.raja.servingStation.service.HistoryOrderService;
import com.raja.servingStation.service.OrderDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhangzhen
 * on 2017/10/10.
 */
@RestController
@ResponseBody
public class HistoryOrderController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(HistoryOrderController.class);
    @Autowired
    private ResponseUtil responseUtil;
    @Autowired
    private HistoryOrderService historyOrderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ClaimService claimService;


    /**
     * 历史工单列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/historyOrder/list",method = RequestMethod.GET)
    public ResponseEntity list(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            Integer stationId = RequestUtil.getUserId(request);
            stationId = 1;
            MysqlPageVo pageVO = historyOrderService.queryOrderListByPage(body,stationId);
            return responseUtil.success(pageVO);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("查询列表异常");
        }
    }

    /**
     * 维修工单详情
     * @param request
     * @return
     */
    @RequestMapping(value = "/historyOrder/getDetailByOrderId",method = RequestMethod.GET)
    public ResponseEntity getDetailByOrderId(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            RepairOrderDetail orderDetail = orderDetailService.getDetailByOrderId(body);
            return responseUtil.success(orderDetail);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("查询列表异常");
        }
    }

    /**
     * 编辑维修工单
     * @param request
     * @return
     */
    @RequestMapping(value = "/historyOrder/editOrderByOrderId",method = RequestMethod.GET)
    public ResponseEntity editOrderByOrderId(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            Object o = orderDetailService.editOrderByOrderId(body);
            return responseUtil.success(o);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("查询列表异常");
        }
    }

    /**
     * 首保工单详情
     * @param request
     * @return
     */
    @RequestMapping(value = "/historyOrder/getSbDetailByOrderId",method = RequestMethod.GET)
    public ResponseEntity getSbDetailByOrderId(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            FirstInsuranceOrder firstInsuranceOrder = orderDetailService.getSbDetailByOrderId(body);
            return responseUtil.success(firstInsuranceOrder);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("查询列表异常");
        }
    }

    /**
     * 首保工单索赔
     * @param request
     * @return
     */
    @RequestMapping(value = "/historyOrder/claimSbOrderByOrderId",method = RequestMethod.GET)
    public ResponseEntity claimSbOrderByOrderId(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            Object o = claimService.claimSbOrderByOrderId(body);
            return responseUtil.success(o);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("首保工单提交索赔异常");
        }
    }

    /**
     * 维修工单索赔
     * @param request
     * @return
     */
    @RequestMapping(value = "/historyOrder/claimRepairOrderByOrderId",method = RequestMethod.GET)
    public ResponseEntity claimRepairOrderByOrderId(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            Object o = claimService.claimRepairOrderByOrderId(body);
            if(o.equals("产生的故障为用户原因，不满足索赔条件！")){
                return responseUtil.exception("产生的故障为用户原因，不满足索赔条件！");
            }
            if(o.equals("车辆当前里程和维修所用零件时长都没有超出保修范围，不满足索赔条件！")){
                return responseUtil.exception("车辆当前里程和维修所用零件时长都没有超出保修范围，不满足索赔条件！");
            }
            if(o.equals("已经发起了索赔，不能再次发起索赔！")){
                return responseUtil.exception("已经发起了索赔，不能再次发起索赔！");
            }
            return responseUtil.success(o);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("维修工单提交索赔异常");
        }
    }


}
